package com.hotbitmapgg.moequest.notice;

import android.content.Context;
import android.hardware.SensorManager;
import android.view.ViewConfiguration;

public class Distance {

  private float mFlingFriction = ViewConfiguration.getScrollFriction();

  private static float DECELERATION_RATE = (float) (Math.log(0.78) / Math.log(0.9));

  private static final float INFLEXION = 0.35f;

  private float mPhysicalCoeff;


  public Distance(Context context) {

    final float ppi = context.getResources().getDisplayMetrics().density * 160.0f;
    mPhysicalCoeff = SensorManager.GRAVITY_EARTH // g (m/s^2)
        * 39.37f // inch/meter
        * ppi
        * 0.84f; // look and feel tuning
  }


  public double getSplineFlingDistance(int velocity) {

    final double l = getSplineDeceleration(velocity);
    final double decelMinusOne = DECELERATION_RATE - 1.0;
    return mFlingFriction * mPhysicalCoeff * Math.exp(DECELERATION_RATE / decelMinusOne * l);
  }


  private double getSplineDeceleration(int velocity) {

    return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff));
  }


  public int getSplineFlingDuration(int velocity) {

    final double l = getSplineDeceleration(velocity);
    final double decelMinusOne = DECELERATION_RATE - 1.0;
    return (int) (1000.0 * Math.exp(l / decelMinusOne));
  }
}
